<?php
/**
 * Created by PhpStorm.
 * User: 共赢岛
 * Date: 2023/12/2
 * Time: 16:36
 */

namespace App\Http\Resources;

use App\Admin\Services\CarService;
use App\Services\LifeCycle\OrderService;
use App\Services\LifeCycle\UserCarsService;
use Illuminate\Http\Resources\Json\JsonResource;

class PackageOrderDetailResource extends  JsonResource
{
    public function toArray($request)
    {
        $user_car = UserCarsService::one(['order_id' => $this->id]);

        if (isset($user_car) && $user_car->car_number) {
            $car_number = $user_car->car_number;
        } else {
            $car_number = '';
        }

        if (in_array($this->package_id, jcbyPackageIds())) {
            $help = "保养有效期为1年，过期后不可使用。";
        }

        if (in_array($this->package_id, bjqPackageIds())) {
            $help = "此券无法叠加使用，每车单面仅限使用一张。";
        }

        return [
            'order_id' => $this->id,
            'package_title' => $this->package->title,
            'attrs' => OrderService::attr($this->id, $this->user_id),
            'created_at' => $this->created_at ? $this->created_at->format('Y-m-d H:i:s') : '',
            'code_url' => path2url($this->code_url),
            'code_number' => $this->code_number,
            'car_number' => $car_number,
            'remarks' => $this->remarks,
            'help' => $help ?? ''
        ];
    }
}